package com.sxd.bill.rela.item;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.sxd.bill.rela.util.Normalizer;

import lombok.Data;

@Data
public class RelationContextItem {
    private String roomName; // 群聊名称
    private String publishDate; // 发布时间
    private String publisherName; // 发布者姓名
    private String publisherNickName; // 发布者微信昵称
    private String publisherOrg; // 发布者所属机构
    private String publisherPhoneNum; // 发布者手机号码
    private String message; // 消息原文
    private String crawlTime; // 消息爬取时间
    private List<CoreRelationItem> coreRelations; // 实体关系列表

    public RelationContextItem() {
        this.coreRelations = new ArrayList<>();
    }

    public void addRelationItem(CoreRelationItem item) {
        coreRelations.add(item);
    }

    public void addAllRelationItems(List<CoreRelationItem> items) {
        coreRelations.addAll(items);
    }

    public int Size() {
        return coreRelations.size();
    }

    /**
     * 获取当前月份
     * 
     * @return
     */
    private Integer getCurMonth() {
        Pattern p = Pattern.compile("\\d+[\\.\\-/年](\\d+)[\\.\\-/月]\\d+日?");
        String[] dts = { publishDate, crawlTime };
        for (String dt : dts) {
            if (dt != null) {
                Matcher m = p.matcher(dt);
                if (m.find()) {
                    String mon = m.group(1);
                    return Integer.parseInt(mon);
                }
            }
        }
        return null;
    }

    /**
     * 对字段进行标准化
     */
    public void normalize() {
        for (CoreRelationItem rela : coreRelations) {
            // 1.贴现人中"我行"替换为"发布者所属结构"
            List<String> discounter = rela.getDiscounter();
            if (publisherOrg != null && publisherOrg.length() > 0) {
                for (int i = 0; i < discounter.size(); i++) {
                    if ("我行".equals(discounter.get(i))) {
                        discounter.set(i, publisherOrg);
                    }
                }
            }
            // 2.交易方向规整
            String trddir = rela.getTradingDirection();
            String normalizedTrddir = Normalizer.normalizeTradingDirection(trddir);
            rela.setTradingDirection(normalizedTrddir);

            // 3.补充票据类型
            List<String> acceptor = rela.getAcceptor();
            Set<String> speciAcceptors = new HashSet<>(Arrays.asList("商票", "财司"));
            if (acceptor.size() > 0) {
                List<String> billType = acceptor.stream().map(acpt -> speciAcceptors.contains(acpt) ? acpt : "银票")
                        .collect(Collectors.toList());
                rela.setBillType(billType);
            }
            // 4.票据期限规整: 调整为 `数字+M` 格式
            List<String> dues = rela.getDueTime();
            Integer curMon = getCurMonth();
            List<String> normalizedDues = Normalizer.normalizeDue(dues, curMon);
            rela.setDueTime(normalizedDues);
        }
    }
}
